package com.sy.module_clockin.utils

import androidx.annotation.IntRange
import com.blankj.utilcode.util.LogUtils
import java.util.*

object TimeUtils {

    /**
     * 获取本月起始结束值
     *
     * @return monthStart monthEnd
     */
    fun getCurrentMonthStartEnd(): Pair<Long, Long> = getMonthStartEnd(Calendar.getInstance())

    /**
     * 获取指定时间月起始结束值
     *
     * @param calendar
     * @return monthStart monthEnd
     */
    fun getMonthStartEnd(calendar: Calendar): Pair<Long, Long> {
        calendar[Calendar.DAY_OF_MONTH] = 1
        calendar[Calendar.HOUR_OF_DAY] = 0
        calendar[Calendar.MINUTE] = 0
        calendar[Calendar.SECOND] = 0
        calendar[Calendar.MILLISECOND] = 0
        val monthStart = calendar.timeInMillis
        calendar[Calendar.MONTH] += 1
        val monthEnd = calendar.timeInMillis - 1
        return Pair(monthStart, monthEnd)
    }


    fun getMonthStartEnd(year: Int, month: Int, day: Int): Pair<Long, Long> {
        val calendar = Calendar.getInstance()
        calendar[Calendar.YEAR] = year
        calendar[Calendar.MONTH] = month
        calendar[Calendar.DAY_OF_MONTH] = day

        calendar[Calendar.DAY_OF_MONTH] = 1
        calendar[Calendar.HOUR_OF_DAY] = 0
        calendar[Calendar.MINUTE] = 0
        calendar[Calendar.SECOND] = 0
        calendar[Calendar.MILLISECOND] = 0
        val monthStart = calendar.timeInMillis
        calendar[Calendar.MONTH] += 1
        val monthEnd = calendar.timeInMillis - 1
        return Pair(monthStart, monthEnd)
    }

    /**
     * 获取今年一月到12月最后一天
     *
     * @return monthStart monthEnd
     */
    fun getCurrentYearMonthStartEnd(): Pair<Long, Long> = getMonthStartEndInYear(Calendar.getInstance())

    /**
     * 获取指定年一月一号到12月最后一天
     *
     * @param calendar
     * @return 一月1到12月最后一天
     */
    fun getMonthStartEndInYear(calendar: Calendar): Pair<Long, Long> {
        calendar[Calendar.MONTH] = 0
        calendar[Calendar.DAY_OF_MONTH] = 1
        val startMonth = calendar.timeInMillis

        calendar[Calendar.MONTH] = 11
        val maxDay = calendar.getMaximum(Calendar.DAY_OF_MONTH)
        calendar[Calendar.DAY_OF_MONTH] = maxDay
        val endMoth = calendar.timeInMillis
        return Pair(startMonth, endMoth)
    }


    /**
     * 获取本周起始结束值
     *
     * @return weekStart weekEnd
     */
    fun getCurrentWeekStartEnd(): Pair<Long, Long> = getWeekStartEnd(Calendar.getInstance())

    /**
     * 获取指定时间周起始结束值
     *
     * @param calendar
     * @return weekStart weekEnd
     */
    fun getWeekStartEnd(calendar: Calendar): Pair<Long, Long> {
        //这个月有几天
        val maxDay = calendar.getMaximum(Calendar.DAY_OF_MONTH)
        //设置本周第一天,第一天按周一开始
        calendar[Calendar.DAY_OF_WEEK] = calendar.firstDayOfWeek
        calendar[Calendar.DAY_OF_MONTH] = calendar[Calendar.DAY_OF_MONTH] + 1

        val weekStart = calendar.timeInMillis
        //这个月有几天-本周第一天=剩余几天
        val d = maxDay - (calendar[Calendar.DAY_OF_MONTH])
        if (d > 7) {
            calendar[Calendar.DAY_OF_MONTH] = calendar[Calendar.DAY_OF_MONTH] + 6
        } else {
            calendar[Calendar.DAY_OF_MONTH] = calendar[Calendar.DAY_OF_MONTH] + d
        }
        val weekEnd = calendar.timeInMillis
        return Pair(weekStart, weekEnd)
    }


    fun getDayStartEnd(year: Int, month: Int, day: Int): Pair<Long, Long> {
        val calendar = Calendar.getInstance()
        calendar[Calendar.YEAR] = year
        calendar[Calendar.MONTH] = month
        calendar[Calendar.DAY_OF_MONTH] = day

        setDayTimeInStart(calendar)
        val dayStart = calendar.timeInMillis
        setDayTimeInEnd(calendar)
        val dayEnd = calendar.timeInMillis
        return Pair(dayStart, dayEnd)
    }

    fun getTodayStartEnd(): Pair<Long, Long> {
        val calendar = Calendar.getInstance()

        setDayTimeInStart(calendar)
        val dayStart = calendar.timeInMillis
        setDayTimeInEnd(calendar)
        val dayEnd = calendar.timeInMillis
        return Pair(dayStart, dayEnd)
    }


    fun isInToday(timeL: Long): Boolean {
        val calendar = Calendar.getInstance()
        val curr = calendar.timeInMillis
        calendar.timeInMillis = timeL
        setDayTimeInStart(calendar)
        val dayStart = calendar.timeInMillis
        setDayTimeInEnd(calendar)
        val dayEnd = calendar.timeInMillis
        return curr in dayStart..dayEnd
    }

    fun getStartEndTime(year: Int, month: Int, day: Int): Pair<Long, Long> {
        val calendar = Calendar.getInstance()
        calendar[Calendar.YEAR] = year
        calendar[Calendar.MONTH] = month - 1
        calendar[Calendar.DAY_OF_MONTH] = day
        calendar[Calendar.HOUR_OF_DAY] = 0
        calendar[Calendar.MINUTE] = 0
        calendar[Calendar.SECOND] = 0
        calendar[Calendar.MILLISECOND] = 0
        val startTime = calendar.timeInMillis
        LogUtils.d("getStartEndTime -- startTime>>  ${calendar.time} ")
        calendar[Calendar.HOUR_OF_DAY] = 24
        val endTime = calendar.timeInMillis - 1
        calendar.timeInMillis = endTime
        LogUtils.d("getStartEndTime -- endTime>>  ${calendar.time} ")
        return startTime to endTime
    }

    /**
     * 获取星期几
     *
     * @param calendar 需要传入 calendar
     * @return 返回星期几
     */
    fun getWeek(calendar: Calendar): String = getWeek(calendar[Calendar.DAY_OF_WEEK])


    /**
     * 获取星期几
     *
     * @param week 取值范围[1,7] 需要传入 calendar[Calendar.DAY_OF_WEEK]
     *@return 返回星期几
     */
    fun getWeek(@IntRange(from = 1, to = 7) week: Int): String = when (week) {
        1 -> "星期日"
        2 -> "星期一"
        3 -> "星期二"
        4 -> "星期三"
        5 -> "星期四"
        6 -> "星期五"
        7 -> "星期六"
        else -> throw IndexOutOfBoundsException("week 取值范围[1,7],当前week=$week")
    }

    /**
     * 将天数转为毫秒
     */
    fun dayToMillisecond(day: Int): Long {
        return day * 24 * 60 * 60 * 1000L
    }


    /**
     *计算当前时间到隔了day天的结束时间
     *
     * @param calendar 开始时间
     * @param day 天数
     * @return Triple(创建时间,开始时间,结束时间)
     */
    fun initDayCalendar(day: Int, calendar: Calendar = Calendar.getInstance()): Triple<Long, Long, Long> {
        val createTime = calendar.timeInMillis
        setDayTimeInStart(calendar)
        val startTime = calendar.timeInMillis
        val endTime = calendar.timeInMillis + dayToMillisecond(day)
        return Triple(createTime, startTime, endTime)
    }

    /**
     * 将时间都设置为0,方便计算
     */
    fun setDayTimeInStart(calendar: Calendar) {
        calendar[Calendar.HOUR_OF_DAY] = 0
        calendar[Calendar.MINUTE] = 0
        calendar[Calendar.SECOND] = 0
        calendar[Calendar.MILLISECOND] = 0
    }


    /**
     * 将时间都设置为23.59.59.999,方便计算
     */
    fun setDayTimeInEnd(calendar: Calendar) {
        calendar[Calendar.HOUR_OF_DAY] = 23
        calendar[Calendar.MINUTE] = 59
        calendar[Calendar.SECOND] = 59
        calendar[Calendar.MILLISECOND] = 999
    }

    /**
     * 转成天数
     */
    fun getDay(distanceTime: Long): Long {
        LogUtils.d("天数$distanceTime")
        return distanceTime / (24 * 60 * 60L * 1000L)
    }

    /**
     *计算当前时间到隔了一年的结束时间
     *
     * @param calendar 开始时间
     * @return Triple(创建时间,开始时间,结束时间)
     */
    fun initOneYearCalendar(calendar: Calendar = Calendar.getInstance()): Triple<Long, Long, Long> {
        val createTime = calendar.timeInMillis
        calendar[Calendar.HOUR_OF_DAY] = 0
        calendar[Calendar.MINUTE] = 0
        calendar[Calendar.SECOND] = 0
        calendar[Calendar.MILLISECOND] = 0
        val startTime = calendar.timeInMillis

        calendar[Calendar.YEAR] += 1
        calendar[Calendar.HOUR_OF_DAY] = 23
        calendar[Calendar.MINUTE] = 59
        calendar[Calendar.SECOND] = 59
        calendar[Calendar.MILLISECOND] = 999
        val endTime = calendar.timeInMillis
        return Triple(createTime, startTime, endTime)
    }

    fun formatYmd(longTime: Long): String {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = longTime
        return String.format("%s-%s-%s", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH))
    }


    fun formatYm(longTime: Long): String {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = longTime
        return String.format("%s-%s", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1)
    }
}